package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R addSetmeal(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R pageSelect(int page,int pageSize,String name){
        //新建分页查询，传入参数
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        //setmeal不满足，新建dto
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //条件查询
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询
        queryWrapper.like(name!=null,Setmeal::getName,name);
        //排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,queryWrapper);
        //排除基本数据
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        //获取表基本数据
        List<Setmeal> records = setmealPage.getRecords();
        //通过stream流方式
        List<SetmealDto> list  = records.stream().map((item) ->{
            SetmealDto setmealDto = new SetmealDto();
            //将item基本属性拷贝到dto
            BeanUtils.copyProperties(item,setmealDto);
            //获取分类id
            Long categoryId = item.getCategoryId();
            //根据id获取对象
            Category category = categoryService.getById(categoryId);
            //判断
            if(category != null){
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);

    }
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
       /* 请求url地址：/dish    ?ids=1504708761831292929,1504653856504483842

        请求方式：DELETE

        请求参数：?ids=1504708761831292929,1504653856504483842

        响应结果【格式统一】：R 对象*/
        setmealService.removeByIds(ids);

        return R.success("删除成功");

    }
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids) {
        // update dish set status = ? where id in (1,2,3)
        LambdaUpdateWrapper<Setmeal> qw = new LambdaUpdateWrapper<>();
        qw.set(Setmeal::getStatus, status);// set status = ?
        qw.in(Setmeal::getId, ids);//  where id in (1,2,3)
        setmealService.update(qw);

        // update dish set status = ? where id = ?
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("修改成功");
    }
    @GetMapping("/{id}")
    public R update(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }
    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.updateByIdWithDish(setmealDto);
        return R.success("修改菜单成功");
    }
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
